package com.ldn.server.mqtt.handler;

import com.ldn.server.mqtt.covert.MqttCovert;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.mica.mqtt.core.server.auth.IMqttServerAuthHandler;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.tio.core.ChannelContext;

/**
 * 自定义的MQTT 链接认证处理
 *
 * @author 李大牛家的牛 (scafel@163.com)
 * @product serverless
 * @className AuthHandler
 * @createDate 2025/2/8 - 16:36
 * @description 链接认证处理
 **/
@Slf4j
@Configuration(proxyBeanMethods = false)
public class AuthHandler implements IMqttServerAuthHandler {

    @Resource
    private ApplicationContext applicationContext;

    @Override
    public boolean authenticate(ChannelContext context, String uniqueId, String clientId, String userName, String password) {
        log.info("authenticate: uniqueId={}, clientId={}, userName={}, password={}", uniqueId, clientId, userName, password);
        MqttCovert covert = applicationContext.getBean(MqttCovert.class);
        return covert.checkAuthenticate(uniqueId, userName, password);
    }
}
